<!--********************************************************************
* Copyright© 2000 - 2018 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title data-i18n="resources.title_densityAnalystService"></title>
    <style type="text/css">
        body {
            margin: 0;
            overflow: hidden;
            background: #fff;
            width: 100%;
            height: 100%
        }

        #map {
            position: absolute;
            width: 100%;
            height: 100%;
            border: 1px solid #3473b7;
        }

        #toolbar {
            position: absolute;
            top: 50px;
            right: 10px;
            text-align: center;
            z-index: 100;
            border-radius: 4px;
        }
    </style>
</head>
<body>
<div id="toolbar" class="panel panel-primary">
    <div class='panel-heading'>
        <h5 class='panel-title text-center' data-i18n="resources.text_densityAnalystService"></h5></div>
    <div class='panel-body content'>
        <input type="button" class="btn btn-default" data-i18n="[value]resources.text_input_value_densityAnalyst" onclick="addResultLayer()"/>&nbsp;
        <input type="button" class="btn btn-default" data-i18n="[value]resources.text_input_value_removeTheme" onclick="removeTheme()"/>
    </div>
</div>
<div id="map"></div>
<script type="text/javascript" include="bootstrap,widgets.alert" src="../js/include-web.js"></script>
<script type="text/javascript" exclude="iclient-classic" src="../../dist/classic/include-classic.js"></script>
<script>
    var map, baseLayer, resultLayer,
        host = window.isLocal ? window.server : "http://iclsvr.supermap.io",
        url = host + "/iserver/services/map-changchun/rest/maps/长春市区图",
        url2 = host + "/iserver/services/spatialanalyst-changchun/restjsr/spatialanalyst";

    init();

    function init() {
        map = new SuperMap.Map("map", {
            controls: [
                new SuperMap.Control.ScaleLine(),
                new SuperMap.Control.PanZoomBar(),
                new SuperMap.Control.Navigation({
                    dragPanOptions: {
                        enableKinetic: true
                    }
                })]
        });
        map.addControl(new SuperMap.Control.LayerSwitcher(), new SuperMap.Pixel(58, 150));

        //将“长春市区图”置为底图
        baseLayer = new SuperMap.Layer.TiledDynamicRESTLayer("长春市区图", url, {
            transparent: true,
            cacheEnabled: true
        }, {maxResolution: "auto"});
        baseLayer.events.on({"layerInitialized": addBaseLayer});
    }

    function addBaseLayer() {
        map.addLayer(baseLayer);
        map.setCenter(new SuperMap.LonLat(5800, -3000), 2);
        map.allOverlays = true;
    }

    function addResultLayer() {
        widgets.alert.clearAlert();
        //移除核密度分析的结果专题图层
        removeTheme();

        //创建一个核密度分析服务实例
        var densityAnalystService = new SuperMap.REST.DensityAnalystService(url2, {
            eventListeners: {
                "processCompleted": KernelAnalystCompleted,
                "processFailed": KernelAnalystFailed
            }
        });

        //创建一个核密度分析参数示例
        var densityKernelAnalystParameters = new SuperMap.REST.DensityKernelAnalystParameters({
            //指定数据集
            dataset: "Railway@Changchun",
            //指定范围
            bounds: new SuperMap.Bounds(3800, -3800, 8200, -2200),
            //指定数据集中用于核密度分析的字段
            fieldName: "SmLength",
            searchRadius: 50,  //Railway@Changchun的单位是米
            //结果数据集名称
            resultGridName: "KernelDensity_Result",
            deleteExistResultDataset: true
        });

        densityAnalystService.processAsync(densityKernelAnalystParameters);
    }

    //用栅格专题图展示分析结果
    function KernelAnalystCompleted(densityKernelAnalysEventArgs) {
        var color1 = new SuperMap.REST.ServerColor(255, 212, 170),
            color2 = new SuperMap.REST.ServerColor(255, 127, 0),
            color3 = new SuperMap.REST.ServerColor(191, 95, 0),
            color4 = new SuperMap.REST.ServerColor(255, 0, 0),
            color5 = new SuperMap.REST.ServerColor(191, 0, 0),

            themeGridRangeIteme1 = new SuperMap.REST.ThemeGridRangeItem({
                start: 0,
                end: 0.05,
                color: color1
            }),
            themeGridRangeIteme2 = new SuperMap.REST.ThemeGridRangeItem({
                start: 0.05,
                end: 5,
                color: color2
            }),
            themeGridRangeIteme3 = new SuperMap.REST.ThemeGridRangeItem({
                start: 5,
                end: 10,
                color: color3
            }),
            themeGridRangeIteme4 = new SuperMap.REST.ThemeGridRangeItem({
                start: 10,
                end: 100,
                color: color4
            }),
            themeGridRangeIteme5 = new SuperMap.REST.ThemeGridRangeItem({
                start: 100,
                end: 360,
                color: color5
            }),

            themeGridRange = new SuperMap.REST.ThemeGridRange({
                reverseColor: false,
                rangeMode: SuperMap.REST.RangeMode.EQUALINTERVAL,
                //栅格分段专题图子项数组
                items: [themeGridRangeIteme1,
                    themeGridRangeIteme2,
                    themeGridRangeIteme3,
                    themeGridRangeIteme4,
                    themeGridRangeIteme5
                ]
            }),
            themeParameters = new SuperMap.REST.ThemeParameters({
                //制作专题图的数据集（核密度分析的结果数据集）
                datasetNames: [densityKernelAnalysEventArgs.result.dataset.split('@')[0]],
                dataSourceNames: ["Changchun"],
                joinItems: null,
                themes: [themeGridRange],
                types: ['REGION']
            });
        var themeService = new SuperMap.REST.ThemeService(url, {
            eventListeners: {
                "processCompleted": themeCompleted,
                "processFailed": themeFailed
            }
        });
        themeService.processAsync(themeParameters);
    }

    function themeCompleted(themeEventArgs) {
        if (themeEventArgs.result.resourceInfo.id) {
            resultLayer = new SuperMap.Layer.TiledDynamicRESTLayer("核密度分析结果", url, {
                cacheEnabled: false,
                transparent: true,
                layersID: themeEventArgs.result.resourceInfo.id
            }, {"maxResolution": "auto"});
            resultLayer.events.on({"layerInitialized": addThemelayer});
            resultLayer.setOpacity(0.8);
        }
    }

    function addThemelayer() {
        //将专题图添加到地图
        map.addLayer(resultLayer);
    }

    function themeFailed(serviceFailedEventArgs) {
        widgets.alert.showAlert(serviceFailedEventArgs.error.errorMsg,false);
    }

    function KernelAnalystFailed(serviceFailedEventArgs) {
        widgets.alert.showAlert(serviceFailedEventArgs.error.errorMsg,false);
    }

    //移除专题图
    function removeTheme() {
        widgets.alert.clearAlert();
        if (map.layers.length > 1) {
            map.removeLayer(resultLayer, true);
        }
    }

</script>

</body>
</html>